package com.miracle.test;


import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class 判断对象类型 {
    public static void main(String[] args) {
        test1();

        reflect();
    }

    public static void reflect() {
        Class mapClass = Map.class;
        try {
            Map map = null;
            if (Modifier.isAbstract(mapClass.getModifiers()) || mapClass.isInterface()){
                map = new HashMap();
            }else {
                map = (Map) mapClass.newInstance();
            }
            map.put("1","tt");
            System.out.println(map);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }

    public static void test1(){
        ConcurrentHashMap<Object, Object> objectObjectConcurrentHashMap = new ConcurrentHashMap<>();
        boolean b = objectObjectConcurrentHashMap instanceof Map;//判断第一个对象是不是第二个的子类
        System.out.println(b);

        Class<? extends ConcurrentHashMap> aClass = objectObjectConcurrentHashMap.getClass();
        Class<ConcurrentHashMap> concurrentHashMapClass = ConcurrentHashMap.class;
        boolean b1 = Map.class.isAssignableFrom(aClass);//判断第一个class是不是第二个class的父类
        System.out.println(b1);
    }
}
